//
//  ShareStatisticsViewController.m
//  SelfDriving
//
//  Created by apple on 16/1/16.
//  Copyright © 2016年 apple. All rights reserved.
//

//xhw2018-1-16：里面注释了的地方都是xhw改
#import "ShareStatisticsViewController.h"
#import "UIDatePickerView.h"
#import "BusinessService+WD.h"
@interface ShareStatisticsViewController ()<UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate,UIDatePickerViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *table;

@end
@implementation ShareStatisticsViewController{
    NSString *stime;
    NSString *etime;
    NSMutableArray *data;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSDate *today = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *date =  [dateFormatter stringFromDate:today];
//    stime = date;
    etime = date;
    self.navigationItem.title = _type;
    
    
    NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:today];//前一天
    NSString *eate =  [dateFormatter stringFromDate:lastDay];
//    etime = eate;
    stime = eate;
    
    [_table reloadData];

    
    
    [self initData];
}
-(void)initData{
    [SVProgressHUD showWithStatus:@"加载中..."];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSNumber numberWithInteger:[ValidateHelper cTimestampFromString:[NSString stringWithFormat:@"%@ 0:0:0",stime] format:@"yyyy-MM-dd HH:mm:ss"]] forKey:@"starttime"];
    [dic setObject:[NSNumber numberWithInteger:[ValidateHelper cTimestampFromString:[NSString stringWithFormat:@"%@ 0:0:0",etime] format:@"yyyy-MM-dd HH:mm:ss"]] forKey:@"endtime"];
    NSLog(@"结束时间xhw%@",[NSNumber numberWithInteger:[ValidateHelper cTimestampFromString:[NSString stringWithFormat:@"%@ 0:0:0",etime] format:@"yyyy-MM-dd HH:mm:ss"]]);
    NSLog(@"开始时间xhw%@",[NSNumber numberWithInteger:[ValidateHelper cTimestampFromString:[NSString stringWithFormat:@"%@ 0:0:0",stime] format:@"yyyy-MM-dd HH:mm:ss"]]);
    NSLog(@"%@",stime);
    NSLog(@"%@",etime);
    if([_type isEqualToString:@"举报统计"]){
        [[BusinessService shareInstance]jubao_statistics:dic success:^(id result) {
            [SVProgressHUD dismiss];
            data = result;
            [_table reloadData];
        } failure:^(id result) {
            [SVProgressHUD showErrorWithStatus:result];
        }];
    }else if ([_type isEqualToString:@"分享统计"]){
        [[BusinessService shareInstance]share_statistics:dic success:^(id result) {
            [SVProgressHUD dismiss];
            data = result;
            [_table reloadData];
        } failure:^(id result) {
            [SVProgressHUD showErrorWithStatus:result];
        }];
    
    }else if ([_type isEqualToString:@"有奖打卡"]){
        [[BusinessService shareInstance]award_daka:dic success:^(id result) {
            [SVProgressHUD dismiss];
            data = result;
            [_table reloadData];
        } failure:^(id result) {
            [SVProgressHUD showErrorWithStatus:result];
        }];
        
    }
    
    
   
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return data.count+1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section!=0) {
        return  8;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section !=0) {
        return 230;
    }else{
        return 168;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.backgroundColor = colorBG;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIButton *btn = (UIButton *)[cell viewWithTag:10];
        btn.layer.cornerRadius = 4;
        [btn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *label = (UILabel *)[self.view viewWithTag:40];
        UILabel *elabel = (UILabel *)[self.view viewWithTag:41];
        if (label.text.length==0) {
//            label.text = etime;
            label.text = stime;

        }
        if (elabel.text.length==0) {
//            elabel.text = stime;
            elabel.text = etime;
        }
        return cell;
    }
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell2"];
    UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCRE_WIDTH, 230)];
    NSString *documentLocation =  [[NSBundle mainBundle]  pathForResource:@"networkshare_q" ofType:@"html"];
    web.tag = indexPath.section;
    NSURL *myDocument = [NSURL fileURLWithPath:documentLocation];
    NSURLRequest *request = [NSURLRequest requestWithURL:myDocument];
    web.backgroundColor = [UIColor clearColor];
    web.delegate = self;
    [web loadRequest:request];
    web.scrollView.scrollEnabled = NO;
    [cell.contentView addSubview:web];
    
    return cell;
    
    
    return nil;
    
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    //              show(['2015-09-02','2015-09-02','2015-09-02','2015-09-02'],[12,33,44,22],'QQ');
    LoginResponseModel *model = [data objectAtIndex:webView.tag-1];
    NSMutableString *hdata = [[NSMutableString alloc]initWithString:@"["];
    NSMutableString *hdata2 = [[NSMutableString alloc]initWithString:@"["];
    
    for (int i = 0; i<model.list.count; i++) {
        NSMutableDictionary *dic = [model.list objectAtIndex:i];
        [hdata appendFormat:@"'%@',",[ValidateHelper dateFromDouble:[[dic objectForKey:@"createtime"] doubleValue]]];
//        [hdata2 appendFormat:@"%.0lf,",[[dic objectForKey:@"total"] floatValue]];
        [hdata2 appendFormat:@"%d,",[[dic objectForKey:@"total"] intValue]];

    }
    [hdata2 appendFormat:@"]"];
    [hdata appendFormat:@"]"];
    
    NSString *tete = [NSString stringWithFormat:@"show(%@,%@,%@);",hdata,hdata2,[NSString stringWithFormat:@"'%@(%@)'",model.name,model.desc]];
    NSLog(@"%@d1:/n%@d2:",hdata,hdata2);
    [webView stringByEvaluatingJavaScriptFromString:tete];
    
}
- (IBAction)timeAction:(UIButton *)sender {
    if (sender.tag == 20) {
        UIDatePickerView  *datePickerView = [[UIDatePickerView alloc] initWithFrame:CGRectMake(15, SCRE_HEIGHT, SCRE_WIDTH-30, 180)];
        datePickerView.backgroundColor = [UIColor whiteColor];
        datePickerView.delegate = self;
        datePickerView.lanel_title.text = @"开始时间";
        [datePickerView up];
        datePickerView.tag = sender.tag+10;
        [[[[UIApplication sharedApplication] windows] firstObject] addSubview:datePickerView];
        
    }else if (sender.tag == 21){
        UIDatePickerView  *datePickerView = [[UIDatePickerView alloc] initWithFrame:CGRectMake(15, SCRE_HEIGHT, SCRE_WIDTH-30, 180)];
        datePickerView.backgroundColor = [UIColor whiteColor];
        datePickerView.delegate = self;
        datePickerView.lanel_title.text = @"开始时间";
        [datePickerView up];
        datePickerView.tag = sender.tag+10;
        [[[[UIApplication sharedApplication] windows] firstObject] addSubview:datePickerView];
        
    }
}
- (void)didSelectDate:(UIDatePickerView *)pickerView Date:(NSMutableDictionary *)date
{

    if (pickerView.tag == 30) {
        UILabel *label = (UILabel *)[self.view viewWithTag:40];
//        label.text = [date objectForKey:@"date"];
        label.text = [date objectForKey:@"dates"];
        stime = [date objectForKey:@"dates"];
    }else if (pickerView.tag == 31){
        UILabel *label = (UILabel *)[self.view viewWithTag:41];
//        label.text = [date objectForKey:@"date"];
        label.text = [date objectForKey:@"dates"];
        etime = [date objectForKey:@"dates"];
        
    }
    pickerView.hidden = YES;
}
- (IBAction)selectAction:(UIButton *)sender {
    [self initData];
}
- (IBAction)backActino:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


@end
